package cn.bdqn.fragment03;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class ListFragment extends Fragment {
    //实例化组件
    private ListView lvList;
    private String[] data = {"android", "IOS", "WP"};
    private OnItemClickListener onItemClickListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_list, container, false);
        lvList = view.findViewById(R.id.lv_list);
        ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, data);
        lvList.setAdapter(adapter);
        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                /*Toast.makeText(getActivity(), ""+position, Toast.LENGTH_SHORT).show();*/
                onItemClickListener.seleted(position);
            }
        });
        return view;
    }


    //第五步
    interface OnItemClickListener {
        void seleted(int i);
    }


    //绑定——-上下文赋值
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        onItemClickListener=(OnItemClickListener) context;
    }
}
